import QtQuick 1.1
import com.nokia.meego 1.0

PageStackWindow {
    id: appWindow
    initialPage: editorPage
    showStatusBar: false
    showToolBar: false

    function currentFilePath() {
        return editor.currentFilePath;
    }

    property string captionText: '';
    function setCaptionText(text) {
        if (text === null || text === undefined) {
            appWindow.captionText = '';
        } else {
            appWindow.captionText = text;
        }
    }

    function loadFile(file) {
        var component = Qt.createComponent(file)
        if (component.status != Component.Ready) {
            console.log("Error loading component:", component.errorString());
            return null;
        }
        var obj = component.createObject(parent, {});
        console.log("Loaded component:", component, " and created an intance of object:", obj);
        return obj
    }

    function showMessageBox(caption, text, yesVisible) {
        var m = appWindow.loadFile("MessageBox.qml");
        if (m !== null) {
            m.setCaptionLabel(caption);
            m.setTextLabel(text);
            m.setYesButtonVisible(yesVisible);
            m.visualParent = appWindow
            m.open();
        }
        return m;
    }

    function pageStackContains(page) {
        function findIterator(p) {
            return p == page;
        }
        return appWindow.pageStack.find(findIterator) !== null;
    }

    function showEditorPage() {
        appWindow.pageStack.pop(editorPage);
        if (appWindow.pageStack.currentPage !== editorPage)
            throw 'appWindow.showEditorPage() Unexpected currentPage=' + appWindow.pageStack.currentPage;
        if (appWindow.pageStack.depth !== 1)
            throw 'appWindow.showEditorPage() Unexpected number of pages=' + appWindow.pageStack.depth;
    }

    property variant editor: editorPage;
    onEditorChanged: {
        console.log('appWindow.onEditorChanged editor=' + editor);
    }

    EditorPage {
        id: editorPage
    }

    function currentPageChanged() {
        //editButtons.visible = pageStack.currentPage === editorPage
        //captionText.visible = !editButtons.visible
    }

    Component.onCompleted: {
        pageStack.currentPageChanged.connect(currentPageChanged)

        /*
        var editToolbarVisible = Settings.value('editToolbarVisible');
        if (editToolbarVisible !== null && editToolbarVisible !== undefined && editToolbarVisible !== editorPage.editToolbarVisible) {
            editorPage.editToolbarVisible = editToolbarVisible;
            editButton.checked = editToolbarVisible;
        }
        */
    }
}
